package com.xxgc.eow.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xxgc.eow.entity.User;
import com.xxgc.eow.mapper.UserMapper;
import com.xxgc.eow.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public User userLogin(String telephone, String password) {
        if(userMapper.selectUserTelephone(telephone).size()==1){
            if (userMapper.selectUserTelephoneAndPassword(telephone,password) != null){
                return userMapper.selectUserTelephoneAndPassword(telephone,password);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /*
    *   1 添加成功
    *   0 手机号被注册
    * */
    @Override
    public int insertUser(String telephone,String password) {
        if (userMapper.selectUserTelephone(telephone).size()==0){
            User user = new User();
            user.setUserName("默认名字");
            user.setUserPassword(password);
            user.setUserUrl("https://img0.baidu.com/it/u=3425868493,3104015061&fm=253&fmt=auto&app=120&f=JPEG?w=1199&h=800");
            user.setNickname("默认");
            user.setSex("男");
            String p="yyyy-MM-dd HH:mm:ss";
            String format = "yyyy-MM-dd";
            SimpleDateFormat sformat=new SimpleDateFormat(p);
            String time2=sformat.format(System.currentTimeMillis());
            DateTime parse = DateUtil.parse(time2, format);
            Instant instant = parse.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate localDate = instant.atZone(zoneId).toLocalDate();

            user.setBirthday(localDate);
            user.setTelephone(telephone);
            user.setPersonalProfile("默认");
            user.setUserRegister(localDate);
            user.setWxNum(11111111);

            LocalDateTime time = LocalDateTime.now();
            user.setCreateTime(time);

            user.setDestroyTime(null);
            user.setTip(1);
            return userMapper.insertUser(user);
        }else{
            return 0;
        }

    }

    @Override
    public User getUserByNameAndPass(User user) {
        return userMapper.selectUserByNameAndPass(user);
    }

    @Override
    public List<User> getByKeyWord(String keyWord) {
        return userMapper.selectBykeyWord(keyWord);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }
}
